package com.hfmall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hfmall.common.GoodsVo;
import com.hfmall.entity.*;
import com.hfmall.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liclo
 * @since 2021-08-12
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("goods")
public class HfGoodsController {
    @Resource
    private HfGoodsServiceImpl hfGoodsService; //商品
    @Resource
    private UtilGoodsVOServiceImpl utilGoodsVOService;
    @Resource
    private HfPicturesServiceImpl hfPicturesService;

    @RequestMapping("goodsinfoWithList")  //根据商品共性-id查询商品
    public ResponseCode<List<HfGoods>> getGoodsByPid(int pid) {
        if (pid < 1) {
            return new ResponseCode(403, "警告:禁止非法输入");//防止通过地址栏输入错误pid
        } else {
            List<HfGoods> goodsList = hfGoodsService.getHfGoodsByInfoId(pid); //根据分类id(3级分类id)-查询对应的具体商品
            goodsList.forEach(g -> { //一个具体商品
                List<UtilGoodsVO> goodsVOS = utilGoodsVOService.getGoodsClassById(g.getId()); //此为一个商品;数组中:一条规格-值 对应一条数据
                Map<String, String> goodsClassValue = new LinkedHashMap<>();
                goodsVOS.forEach(gv -> {
                    goodsClassValue.put(gv.getClassName(), gv.getValue());
                });
                g.setGoodsClassValue(goodsClassValue);
                List<HfPictures> picturesList = hfPicturesService.getHfPicturesByGoodsId(g.getId()); //根据商品id查询图片组
                if (picturesList != null) {
                    g.setHfPictureList(picturesList);
                }

            });
//            log.debug("商品查询:-----{}", goodsList);
            return new ResponseCode<>(goodsList);
        }

    }

    /**
     * 查询sku信息
     *
     * @author liclo
     * @date 2021/8/19
     */
    @GetMapping("getSku")
    public ResponseCode<List<GoodsVo>> getSkuById(Integer id) {
        return new ResponseCode(hfGoodsService.getSkuById(id));
    }

    /**
     * 更新sku信息
     *
     * @author liclo
     * @date 2021/8/20
     */
    @PostMapping("updateSku")
    public ResponseCode updateSku(@RequestBody JSONObject json) {
        JSONObject data = JSON.parseObject(json.toJSONString());
        Integer id = Integer.valueOf(data.getString("id"));
        List<GoodsVo> goodsVos = JSON.parseArray(data.getString("goodsVos"), GoodsVo.class);

        for (int i = 0; i < goodsVos.size(); i++) {
            UpdateWrapper<HfGoods> wrapper = new UpdateWrapper<>();
            GoodsVo goodsVo = goodsVos.get(i);
            wrapper.set("price", goodsVo.getPrice())
                    .set("stock", goodsVo.getStock())
                    .eq("goodsinfo_id", id)
                    .eq("sku", goodsVo.getSku());
            hfGoodsService.update(wrapper);
        }

        return new ResponseCode();
    }
}
